package com.cls.business.strategy.impl.export;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.cls.business.strategy.ExpertExcel8ReviewRowStrategy;
import com.cls.business.vo.review.result.DeclTopicVO;
import com.cls.business.vo.review.result.ExpertGroupVO;
import com.cls.business.vo.review.result.ExpertScoreVO;
import com.cls.business.vo.review.result.ReviewResultExportVO;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 这个是评审结果统计表的自定义
 */
@Service("reviewExportExcelImpl")
public class ReviewExportExcelImpl implements ExpertExcel8ReviewRowStrategy {

    @Override
    public void writeDeclTopic(ExcelWriter writer, LinkedList<DeclTopicVO> declTopics, int reviewResultIndex, int expertSize) {
        CellStyle cellStyle = writer.getCellStyle();
        Font font = writer.createFont();
        font.setFontName("宋体");
        font.setBold(false);
        font.setFontHeightInPoints((short) 10);
        cellStyle.setFont(font);

        //循环每一行数据
        for (int declTopicIndex = 0; declTopicIndex < declTopics.size(); declTopicIndex++) {
            DeclTopicVO declTopicVO = declTopics.get(declTopicIndex);
            ArrayList<Serializable> declTopicRow = CollUtil.newArrayList((reviewResultIndex + 1) + "-" + (declTopicIndex + 1), declTopicVO.getDeclTopicName(), declTopicVO.getUserName(), declTopicVO.getZwzc());
            StringBuffer opinion = new StringBuffer();
            for (int i = 0; i < expertSize; i++) {
                LinkedList<ExpertScoreVO> scores = declTopicVO.getScores();
                if (scores.size() > i) {
                    ExpertScoreVO expertScoreVO = scores.get(i);
                    declTopicRow.add(expertScoreVO.getScore());
                    opinion.append(expertScoreVO.getExpertName() + ": " + expertScoreVO.getOpinion() + "\n");
                } else {
                    declTopicRow.add("");
                }
            }
            declTopicRow.add(declTopicVO.getPlanScore());
            declTopicRow.add(opinion.toString());
            writer.writeRow(declTopicRow);
            writer.setRowHeight(writer.getCurrentRow() - 1, 30);
        }
    }

    /**
     * 这没有二级  所以啥也不实现
     *
     * @param reviewResultExportVO 当前的课题
     * @param expertGroupVO        专家分组
     * @param expertGropsIndex     当前的index
     */
    @Override
    public void writeExpertGroupTitle(ExcelWriter writer, ReviewResultExportVO reviewResultExportVO, ExpertGroupVO expertGroupVO, int expertGropsIndex, int headMergeSize,CellStyle expertGroupTitleCellStyle) {

    }

    /**
     * 写文章的总标题，他没有可以不写
     *
     * @param excelWriter
     * @param headMergeSize
     */
    @Override
    public void writeExcleTile(ExcelWriter excelWriter, int headMergeSize,String title) {

    }

    /**
     * 写第一级的大标题  课题
     *
     * @param excelWriter
     * @param headMergeSize
     * @param reviewResultExportVO
     */
    @Override
    public void writeTopicHead(ExcelWriter excelWriter, int headMergeSize, ReviewResultExportVO reviewResultExportVO) {
        //写上标题topicName
        excelWriter.merge(headMergeSize, reviewResultExportVO.getTopicName());
        //注意此处一定要后进行设置高度，否则会设置不成功。详情进去一层看判断
        excelWriter.setRowHeight(excelWriter.getCurrentRow() - 1, 40);
    }


}
